package com.cskaoyan.config4;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author stone
 * @date 2022/08/11 10:42
 */
@Configuration
public class DataSourceConfiguration4 {

    // 从容器中取出这个组件，配置类里的组件注册过程就可以使用该参数组件维护的值 → 这些值是从配置文件中获得的
    /*@Autowired
    DatasourceProperties properties; */

    DatasourceProperties properties;
    // 有参构造方法来完成实例化 → 形参，从容器中默认按照类型取出组件
    public DataSourceConfiguration4(DatasourceProperties properties) {
        this.properties = properties;
    }

    @Bean
    public DataSource dataSource4() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(properties.getDriverClassName());
        dataSource.setUrl(properties.getUrl());
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        return dataSource;
    }
}
